package net.hvacapp.cmvrfforandroid.widget.swipemenulistview;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * @author baoyz
 * @date 2014-8-23
 */
public class SwipeMenuView extends LinearLayout implements OnClickListener {

    private SwipeMenuListView mListView;
    private SwipeMenuLayout mLayout;
    private SwipeMenu mMenu;
    private ImageView mMenuIcon;
    private TextView mMenuText;
    private OnSwipeItemClickListener onItemClickListener;
    private int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    /**
     * 设置菜单的icon
     *
     * @param resource 图片资源id
     */
    public void setMenuIcon(int resource) {
        mMenuIcon.setImageResource(resource);
    }

	/**
	 * 设置菜单文本
     *
	 * @param text 文本内容
	 */
	public void setMenuText(String text) {
		mMenuText.setText(text);
	}

	public SwipeMenuView(SwipeMenu menu, SwipeMenuListView listView) {
		super(menu.getContext());
		mListView = listView;
		mMenu = menu;
		List<SwipeMenuItem> items = menu.getMenuItems();
		int id = 0;
		for (SwipeMenuItem item : items) {
			addItem(item, id++);
		}
	}

    private void addItem(SwipeMenuItem item, int id) {
        LayoutParams params = new LayoutParams(item.getWidth(),
                LayoutParams.MATCH_PARENT);
        LinearLayout parent = new LinearLayout(getContext());
        parent.setId(id);
        parent.setGravity(Gravity.CENTER);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setLayoutParams(params);
        parent.setBackgroundDrawable(item.getBackground());
        parent.setOnClickListener(this);
        addView(parent);

        if (item.getIcon() != null) {
            parent.addView(createIcon(item));
        }
        if (!TextUtils.isEmpty(item.getTitle())) {
            parent.addView(createTitle(item));
        }

    }

    private ImageView createIcon(SwipeMenuItem item) {
        mMenuIcon = new ImageView(getContext());
        mMenuIcon.setImageDrawable(item.getIcon());
        return mMenuIcon;
    }

    private TextView createTitle(SwipeMenuItem item) {
        mMenuText = new TextView(getContext());
        mMenuText.setText(item.getTitle());
        mMenuText.setGravity(Gravity.CENTER);
        mMenuText.setTextSize(item.getTitleSize());
        mMenuText.setTextColor(item.getTitleColor());
        return mMenuText;
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null && mLayout.isOpen()) {
            onItemClickListener.onItemClick(mLayout, this, mMenu, v.getId());
        }
    }

    public OnSwipeItemClickListener getOnSwipeItemClickListener() {
        return onItemClickListener;
    }

    public void setOnSwipeItemClickListener(OnSwipeItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setLayout(SwipeMenuLayout mLayout) {
        this.mLayout = mLayout;
    }

    public static interface OnSwipeItemClickListener {
        void onItemClick(SwipeMenuLayout swipeMenuLayout, SwipeMenuView view, SwipeMenu menu, int index);
    }
	}
